package com.ithiema.reggie.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ithiema.reggie.Mapper.DishMapper;
import com.ithiema.reggie.Service.DishFlavorService;
import com.ithiema.reggie.Service.DishService;
import com.ithiema.reggie.common.CustomException;
import com.ithiema.reggie.dto.DishDto;
import com.ithiema.reggie.entity.Category;
import com.ithiema.reggie.entity.Dish;
import com.ithiema.reggie.entity.DishFlavor;
import com.ithiema.reggie.entity.SetmealDish;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j

public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishService dishService;

    //新增菜品，同时保存对应的口味数据  {保存菜品信息}
    @Override
    @Transactional//事务回滚  {这是多表保存,如果没有保存时就会少存一个表}
    public void saveWithFlavor(DishDto dishDto) {
        //1.保存菜品的基本信息 dish
        save(dishDto);
        //2.保存菜品的口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        //3.给菜品口味设置dishId
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }
        //4. 批量保存菜品口味    saveBatch {保存批次}
        dishFlavorService.saveBatch(flavors);
    }


    //根据id查询菜品信息和对应的口味信息
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息，从dish表查询
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> lwp = new LambdaQueryWrapper<>();
        lwp.eq(DishFlavor::getId,dish.getId());
        List<DishFlavor> list = dishFlavorService.list(lwp);
        //获取 是set
        dishDto.setFlavors(list);

        return dishDto;
    }

    @Override
    @Transactional//事务回滚  {这是多表保存,如果没有保存时就会少存一个表}
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);

        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        //delete from dish_flavor where dish_id = ?
        dishFlavorService.remove(queryWrapper);

        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors.forEach(item -> item.setDishId(dishDto.getId()));

        dishFlavorService.saveBatch(flavors);
    }

    //单独删除,批量删除菜品
    @Override
    public void removeWithDish(List<Long> ids) {
        //两个类中都要删除
        LambdaQueryWrapper<Dish> lwq1 = new LambdaQueryWrapper<>();
        lwq1.in(Dish::getId,ids).eq(Dish::getStatus,1);
        if (count(lwq1)>0){
            throw new CustomException("菜品起售中,不能删除");
        }
        //删除要根据id删除
        removeByIds(ids);

        LambdaQueryWrapper<DishFlavor> lwq2 = new LambdaQueryWrapper<>();
        lwq2.in(DishFlavor::getId,ids);
        dishFlavorService.remove(lwq2);
    }

    //菜品停售,起售
    @Override
    public void salesWithDish(Integer status, List<Long> ids) {
        //LambdaUpdateWrapper<>();这个是专门修改的
        LambdaUpdateWrapper<Dish> lwp = new LambdaUpdateWrapper<>();
        lwp.in(ids!=null,Dish::getId,ids);
        //根据数据进行批量查询
        List<Dish> list = dishService.list(lwp);
        for (Dish dish : list) {
            //如果菜品是停用
            if (dish!=null){
                dish.setStatus(status);
                dishService.updateById(dish);
            }
        }

    }
}